package com.huluobo.a2308douyindemo.register

import android.util.Log
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2308douyindemo.databinding.ActivityRegisterBinding
import com.huluobo.a2308douyindemo.mvvm.BaseActivity
import com.huluobo.a2308douyindemo.net.MessageEvent
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus

@AndroidEntryPoint
class RegisterActivity : BaseActivity<ActivityRegisterBinding, RegisterViewModel>() {
    override fun initData() {
        vm.registerSuccess.observe(this) {
            ToastUtils.showLong("注册成功")
            val json = Gson().toJson(it.data)
            EventBus.getDefault().post(MessageEvent(json))
            finish()
        }
    }

    override fun initView() {
        binding.registerBtn.setOnClickListener {
            val username = binding.registerUsernameEt.text.toString()
            val password = binding.registerPasswordEt.text.toString()
            val rePassword = binding.registerRePasswordEt.text.toString()

            if (username.isNotEmpty() && password.isNotEmpty() && rePassword.isNotEmpty()) {
                if (password == rePassword) {
                    vm.register(username, password)
                } else {
                    binding.registerRePasswordEt.error = "两次密码输入不一致"
                }
            } else {
                binding.registerRePasswordEt.error = "用户名密码输入项不能为空"
            }
        }
    }
}